package com.xxxx.manager.controller;

import com.xxxx.common.result.RespBean;
import com.xxxx.manager.pojo.Brand;
import com.xxxx.manager.pojo.Goods;
import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.pojo.Suppliers;
import com.xxxx.manager.pojo.query.GoodsQuery;
import com.xxxx.manager.service.BrandService;
import com.xxxx.manager.service.GoodsCategoryService;
import com.xxxx.manager.service.GoodsService;
import com.xxxx.manager.service.SuppliersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsCategoryService goodsCategoryService;

    @Resource
    private BrandService brandService;

    @Resource
    private SuppliersService suppliersService;

    @Resource
    private GoodsService goodsService;
    /**
     * to 商品列表页面
     */
    @RequestMapping("list")
    public String toGoodsCategory(Model model) {

        // 查询所有商品分类
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.selectAll();
        model.addAttribute("goodsCategoryList",goodsCategoryList);

        // 查询所有的品牌
        List<Brand> brandList = brandService.selectAllBrand();
        model.addAttribute("brandList",brandList);

        return "goods/list/goods_list";
    }

    /**
     * to 添加新商品页面
     */
    @RequestMapping("addList")
    public String toAddGoodsList(Model model) {
        // 查询一级商品分类
        List<GoodsCategory> goodsCategories = goodsCategoryService.selectOneCategory();
        model.addAttribute("goodsCategories",goodsCategories);

        // 查询所有商品品牌
        List<Brand> brands = brandService.selectAllBrand();
        model.addAttribute("brands",brands);

        // 查询所有供应商
        List<Suppliers> suppliers = suppliersService.selectAllSuppliers();
        model.addAttribute("suppliers",suppliers);

        return "goods/list/add_goods_list";
    }

    /**
     * 级联查询
     */
    @RequestMapping("category/{parentId}")
    @ResponseBody
    public List<GoodsCategory> selectCategory(@PathVariable Short parentId) {

        return goodsCategoryService.selectCategory(parentId);
    }

    /**
     * 保存商品通用信息
     */
    @RequestMapping("save")
    @ResponseBody
    public RespBean saveGoods(Goods goods) {

        goodsService.saveGoods(goods);

        return RespBean.success("保存成功");
    }

    /**
     * 商品列表分页查询
     */
    @RequestMapping("goodsPage")
    @ResponseBody
    public Map<String,Object> queryGoodsList(GoodsQuery goodsQuery, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {

        return goodsService.queryGoodsList(goodsQuery,pageNum,pageSize);
    }

    /**
     * to 商品分类列表页面
     */
    @RequestMapping("category/list")
    public String toCategoryList() {

        return "goods/category/category_list";
    }

    /**
     * to 商品新增分类页面
     */
    @RequestMapping("category/add")
    public String toCategoryAdd() {

        return "goods/category/category_add";
    }

}
